// 生成id---uuid
import { v4 as uuidv4 } from 'uuid';

// 补0函数
const zeroFill = (value: string | number): string => {
  let value1 = String(value);
  if (value1.length === 1) {
    return '0' + value;
  }
  return value1;
};

// 转16进制
const convert = (value: string | number): string => {
  let number1 = Number(`${value}`).toString(16).toUpperCase().padStart(2, '0');
  return number1;
};

// 取个位数
function getLastDigit(num: number): number {
  let lastDigit = num % 10; // 取个位数
  if (lastDigit < 0) {
    lastDigit += 10; // 如果个位数是负数，加上10使其成为正数
  }
  return lastDigit;
}

/**
 *
 * @param buildingId 楼栋id
 * @param floor 楼层
 * @param roomTypeId 房型
 * @param roomNo 房号
 * @param roomName 房名
 * @param lockType 锁类型
 * @param status 房态
 * @param isSuite 是否套房
 * @param subRoomCount 子房间数
 */

interface room {
  buildingId: number;
  floor: string;
  roomTypeId: number;
  roomNo: string;
  roomName: string;
  lockType: number;
  isSuite: boolean;
  subRoomCount: number;
  buildingName: string;
}

export const GenerateRoom = ({
  buildingId,
  floor,
  roomTypeId,
  roomNo,
  roomName,
  lockType,
  isSuite,
  subRoomCount,
  buildingName,
}: room) => {
  // 楼栋-楼层-房号  转16进制
  // 楼栋
  const bId = convert(buildingName);
  // 楼层
  const fId = convert(floor);
  // 房号
  const rId = convert(parseInt(roomNo));

  let idStr = `${bId}${fId}${rId}`;
  if (!isSuite) {
    return {
      id: 0,
      roomId: `${idStr}00`,
      buildingId,
      floor,
      lockType,
      roomName,
      roomNo: `${floor}${zeroFill(parseInt(roomNo))}`,
      roomTypeId,
      isSuite: 0,
      subRoomCount: 0,
      status: 1,
    };
  } else {
    let arr = [];
    arr.push({
      id: uuidv4().replace(/-/g, ''),
      roomId: `${idStr}00`,
      buildingId,
      floor,
      lockType,
      roomName,
      roomNo: `${floor}${zeroFill(parseInt(roomNo))}`,
      roomTypeId,
      mainRoomId: '',
      isSuite: isSuite ? 1 : 0,
      subRoomCount,
      status: 1,
    });
    for (let i = 1; i <= subRoomCount; i++) {
      arr.push({
        id: uuidv4().replace(/-/g, ''),
        roomId: `${idStr}${convert(String(i))}`,
        buildingId,
        floor,
        lockType,
        roomName: `${roomName}-${i}`,
        roomNo: `${floor}${zeroFill(parseInt(roomNo))}${zeroFill(String(i))}`,
        roomTypeId,
        mainRoomId: `${idStr}00`,
        isSuite: 0,
        subRoomCount: 0,
        status: 1,
      });
    }
    return arr;
  }
};

/**
 *
 * @param buildingId 楼栋id
 * @param selectFloor 选择的楼层
 * @param room_type_id 房间类型
 * @param startNumber 起始序号
 * @param endNumber 结束序号
 * @param prefix 前缀
 * @param excludeNumber 排除数
 * @param lockType 锁类型
 * @param isSuite 是否套房
 * @param subRoomCount 子房间数
 */

interface rooms {
  buildingId: any;
  selectFloor: any;
  room_type_id: any;
  startNumber: any;
  endNumber: any;
  prefix?: any;
  excludeNumber?: any;
  lockType: any;
  isSuite?: any;
  subRoomCount?: any;
  buildingName: any;
  roomTypeName: any;
}

export const GenerateRoomList = ({
  buildingId,
  selectFloor,
  room_type_id,
  startNumber,
  endNumber,
  prefix,
  excludeNumber,
  lockType,
  isSuite,
  subRoomCount,
  buildingName,
  roomTypeName,
}: rooms) => {
  console.log("excludeNumber ==> ", excludeNumber)
  let arr: any = [];
  if (!isSuite) {
    for (let i = 0; i < selectFloor.length; i++) {
      for (let start = startNumber; start <= endNumber; start++) {

        // if (excludeNumber && excludeNumber.includes(String(getLastDigit(start)))) {
        //   continue;
        // }
        let shouldContinue = false;
        for (let j = 0; j < excludeNumber.length; j++) {
          console.log(parseInt(excludeNumber[j]) === start);

          if (parseInt(excludeNumber[j]) === start) {
            shouldContinue = true;
            break;
          }
        }
        if (shouldContinue) {
          continue;
        }


        // 楼栋-楼层-房号  转16进制
        // 楼栋
        const bId = convert(buildingName);
         console.log("fId1",parseInt(selectFloor[i]))
        // 楼层
        const fId = convert(parseInt(selectFloor[i]));

        console.log("fId2",fId)
        // 房号
        const rId = convert(parseInt(start));

        let roomId = `${bId}${fId}${rId}`;

        // const roomId = zeroFill(buildingId) + zeroFill(parseInt(selectFloor[i])) + zeroFill(start);
        arr.push({
          id: uuidv4().replace(/-/g, ''),
          roomId: `${roomId}00`,
          buildingId,
          buildingName,
          roomTypeName,
          floor: parseInt(selectFloor[i]),
          lockType,
          roomTypeId: room_type_id,
          roomName: `${prefix ? prefix : ''}${parseInt(selectFloor[i])}${zeroFill(start)}`,
          roomNo: `${parseInt(selectFloor[i])}${zeroFill(start)}`,
          mainRoomId: '',
          isSuite: isSuite ? 1 : 0,
          subRoomCount: subRoomCount || 0,
          status: 1,
        });
      }
    }
  } else {
    for (let i = 0; i < selectFloor.length; i++) {
      for (let start = startNumber; start <= endNumber; start++) {
        // if (excludeNumber && excludeNumber.includes(String(start))) {
        //   continue;
        // }  
        let shouldContinue = false;
        for (let j = 0; j < excludeNumber.length; j++) {
          console.log(parseInt(excludeNumber[j]) === start);

          if (parseInt(excludeNumber[j]) === start) {
            shouldContinue = true;
            break;
          }
        }
        if (shouldContinue) {
          continue;
        }
        // 楼栋-楼层-房号  转16进制
        // 楼栋
        const bId = convert(buildingName);
        // 楼层
        const fId = convert(parseInt(selectFloor[i]));
            
        // 房号
        const rId = convert(parseInt(start));

        let roomId = `${bId}${fId}${rId}`;
        // const roomId = zeroFill(buildingId) + zeroFill(parseInt(selectFloor[i])) + zeroFill(start);
        arr.push({
          id: uuidv4().replace(/-/g, ''),
          roomId: `${roomId}00`,
          buildingId,
          floor: parseInt(selectFloor[i]),
          lockType,
          buildingName,
          roomTypeName,
          roomTypeId: room_type_id,
          roomName: `${prefix ? prefix : ''}${parseInt(selectFloor[i])}${zeroFill(start)}`,
          roomNo: `${parseInt(selectFloor[i])}${zeroFill(start)}`,
          mainRoomId: '',
          isSuite: isSuite ? 1 : 0,
          subRoomCount: subRoomCount || 0,
          status: 1,
        });
        for (let j = 1; j <= subRoomCount; j++) {
          arr.push({
            id: uuidv4().replace(/-/g, ''),
            roomId: `${roomId}${convert(j)}`,
            buildingId,
            buildingName,
            roomTypeName,
            floor: parseInt(selectFloor[i]),
            lockType,
            roomTypeId: room_type_id,
            roomName: `${prefix ? prefix : ''}${parseInt(selectFloor[i])}${zeroFill(start)}-${j}`,
            roomNo: `${parseInt(selectFloor[i])}${zeroFill(start)}${zeroFill(j)}`,
            mainRoomId: `${roomId}00`,
            isSuite: 0,
            subRoomCount: 0,
            status: 1,
          });
        }
      }
    }
  }
  return arr;
};
